package mk.com.ldm.corepoint.implementation.client.lib.generators;

import java.util.HashMap;

import mk.com.ldm.corepoint.implementation.client.CoreServiceAsync;
import mk.com.ldm.corepoint.implementation.client.lib.custom.listview.ListView;
import mk.com.ldm.corepoint.implementation.client.lib.wrappers.filters.DateRangeFilter;
import mk.com.ldm.corepoint.implementation.client.lib.wrappers.filters.ListBoxFilter;
import mk.com.ldm.corepoint.implementation.client.lib.wrappers.filters.TextBoxFilter;
import mk.com.ldm.corepoint.implementation.shared.Filter;
import mk.com.ldm.corepoint.implementation.shared.FormRow;
import mk.com.ldm.corepoint.implementation.shared.TableColumn;

import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class FilterFactory {
	private HashMap<String, Integer> widgetMap = new HashMap<String, Integer>();
	private HashMap<String, Filter> filters = new HashMap<String, Filter>();

	public FilterFactory(HashMap<String, Filter> filters) {
		widgetMap.put("TB", 0); // TextBox
		widgetMap.put("DR", 1); // Date Range
		widgetMap.put("LB", 2); // ListBox
		widgetMap.put("LV", 3); // ListView

		this.filters = filters;
	}

	public Widget getWidget(final TableColumn tc, Button filterBtn,
			CoreServiceAsync rpcService) {
		Integer code = widgetMap.get(tc.getFilterWidget());
		if(code==null){code=999999;}

		Widget filter = new Widget();

		switch (code) {
		case 0:
			filter = new TextBoxFilter(filters, tc, filterBtn);
			break;
		case 1:
			filter = new DateRangeFilter(filters, tc, filterBtn);
			break;
		case 2:
			filter = new ListBoxFilter(filters, tc, filterBtn, rpcService);
			break;
		case 3:
			FormRow config = new FormRow();
			String[] elements = tc.getFilterField().split(",");
			config.setNam("");
			config.setInp("LV");
			config.setDbn(elements[2]);
			config.setTag(elements[2] + "," + elements[3]);
			config.setSiz(tc.getWidth() + "");
			config.setSpl(0);
			config.setPar("");
			config.setVld("");
			config.setEna(1);
			config.setVis(1);
			config.setTab(0);
			config.setEts("");
			config.setVal("");
			config.setFgn("");
			ListView lv = new ListView(config, new HandlerManager(null));
			lv.setEditButtonVisible(false);
			lv.getLabel().setWidth("0px");
			lv.getTextbox().setWidth((tc.getWidth()-38)+"px");
			final TextBox tb = lv.getTextbox();
			tb.addBlurHandler(new BlurHandler() {
				@Override
				public void onBlur(BlurEvent event) {
					if (!tb.getValue().equals("")) {
						HashMap<String, String> hm = new HashMap<String, String>();
						hm.put(tc.getHeader(), tb.getValue());
						Filter f = new Filter(tc.getHeader(), hm, tc
								.getFilterOperator());
						filters.put(tc.getHeader(), f);
					} else {
						filters.remove(tc.getHeader());
					}
				}
			});
			filter = lv;
			break;
		}

		return filter;
	}
}
